//
//  ShowTableViewCell.m
//  Taste
//
//  Created by 徐昊 on 16/9/5.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "ShowTableViewCell.h"
#import "UIView+SDAutoLayout.h"
#import "ShowTableViewCellCommentView.h"
#import "ZWYNetTool.h"
#import "ShowRes.h"
#import "UIImageView+WebCache.h"
#import "NSString+Vali.h"
#import "HXMacro.h"
@interface ShowTableViewCell ()
@property (nonatomic, strong) UIImageView *contentImage;
@property (nonatomic, strong) ShowTableViewCellCommentView *publish;
@property (nonatomic, strong) UILabel *PraiseNumber;
@property (nonatomic, strong) ShowTableViewCellCommentView *comment;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIImageView *love;

@end

@implementation ShowTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setup];
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}
- (void)setRes:(ShowRes *)res {
    _res = res;
    NSArray *array = @[res];
    [_publish setupWithCommentItemsArray:array];
    [_comment setupWithCommentItemsArray:_res.comments];
    [_contentImage sd_setImageWithURL:[NSURL URLWithString:[NSString imageWebWithStr:_res.pic]] placeholderImage:[UIImage imageNamed:@"IMG_Universal_Placeholder"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    }];
    _PraiseNumber.text = [NSString stringWithFormat:@"%@人赞过", _res.praise];
    NSRange range = [_PraiseNumber.text rangeOfString:_PraiseNumber.text];
    [self setTextColor:_PraiseNumber FontNumber:[UIFont systemFontOfSize:12] AndRange:range AndColor:[UIColor blueColor]];
    [self setupAutoHeightWithBottomView:_comment bottomMargin:15];
}
- (void)setup {
    _contentImage = [UIImageView new];
    
    _publish = [ShowTableViewCellCommentView new];
    
    _PraiseNumber = [UILabel new];
    _PraiseNumber.font = [UIFont systemFontOfSize:12];
    
    _love = [UIImageView new];
    _love.image = [UIImage imageNamed:@"LZUniversal_Icon_Like_40_HL"];
    
    _comment = [ShowTableViewCellCommentView new];
    
    _backView = [UIView new];
    
    NSArray *views = @[_contentImage, _publish, _PraiseNumber, _love, _comment, _backView];
    [self.contentView sd_addSubviews:views];
    
    UIView *contentView = self.contentView;
    CGFloat margin = 10;
    
    _contentImage.sd_layout
    .leftSpaceToView(contentView, 0)
    .rightSpaceToView(contentView, 0)
    .topSpaceToView(contentView, 0)
    .heightEqualToWidth();
    _contentImage.backgroundColor = [UIColor redColor];
    
    _backView.sd_layout
    .leftSpaceToView(contentView, margin)
    .rightSpaceToView(contentView, margin)
    .topSpaceToView(_contentImage, 0)
    .heightIs(40);
    
    UIImageView *praise = [UIImageView new];
    UIImageView *commentImg = [UIImageView new];
    UIImageView *more = [UIImageView new];
    [_backView sd_addSubviews:@[praise, commentImg, more]];
    praise.sd_layout
    .leftSpaceToView(_backView, 0)
    .centerYEqualToView(_backView)
    .heightRatioToView(_backView, 0.8)
    .widthEqualToHeight();
    praise.image = [UIImage imageNamed:@"LZUniversal_Icon_Like_120"];
    
    commentImg.sd_layout
    .leftSpaceToView(praise, 30)
    .heightRatioToView(praise, 1)
    .widthEqualToHeight()
    .centerYEqualToView(praise);
    commentImg.image = [UIImage imageNamed:@"LZUniversal_Icon_Recommend_120"];
    
    more.sd_layout
    .rightSpaceToView(_backView, margin)
    .centerYEqualToView(_backView)
    .heightRatioToView(commentImg, 1)
    .widthEqualToHeight();
    more.image = [UIImage imageNamed:@"LZUniversal_Icon_More_120"];
    
    UIView *line = [UIView new];
    line.backgroundColor = [UIColor grayColor];
    [contentView addSubview:line];
    line.sd_layout
    .leftEqualToView(_backView)
    .rightEqualToView(_backView)
    .topSpaceToView(_backView, 1)
    .heightIs(1);
    
    // publish 在内部进行高度自适应
    _publish.sd_layout
    .leftEqualToView(line)
    .rightEqualToView(line)
    .topSpaceToView(line, margin);
    
    // 喜欢那颗心
    _love.sd_layout
    .leftSpaceToView(self.contentView, margin + 10)
    .topSpaceToView(_publish, 0)
    .heightIs(18)
    .widthEqualToHeight();
    
    _PraiseNumber.sd_layout
    .leftSpaceToView(_love, margin)
    .topSpaceToView(_publish, 0)
    .heightIs(18);
    [_PraiseNumber setSingleLineAutoResizeWithMaxWidth:100];
    
    // 同样 comment 在内部进行高度自适应
    _comment.sd_layout
    .leftEqualToView(line)
    .rightEqualToView(line)
    .topSpaceToView(_PraiseNumber, 0);
    
//    UIView *bottomView = nil;
//    if (_res.comments.count == 0) {
//        bottomView = _PraiseNumber;
//    } else {
//        bottomView = _comment;
//    }
//    [self setupAutoHeightWithBottomView:bottomView bottomMargin:5];
}
//设置不同字体颜色
-(void)setTextColor:(UILabel *)label FontNumber:(id)font AndRange:(NSRange)range AndColor:(UIColor *)vaColor
{
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:label.text];
    //设置字号
    [str addAttribute:NSFontAttributeName value:font range:range];
    //设置文字颜色
    [str addAttribute:NSForegroundColorAttributeName value:vaColor range:range];
    
    label.attributedText = str;
}
- (void)awakeFromNib {
    // Initialization code
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
